/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.settings.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.gui.panel.AbstractPanel;
import com.genia.toolbox.basics.editor.model.document.VariableModel;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.gui.view.internal.renderer.ComboBoxRenderer;
import com.genia.toolbox.form.editor.gui.view.settings.table.VariableCellEditor;
import com.genia.toolbox.form.editor.gui.view.settings.table.VariableCellRenderer;
import com.genia.toolbox.form.editor.gui.view.settings.table.VariableTableModel;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.web.gwt.form.server.description.Description;
import com.genia.toolbox.web.gwt.form.server.description.validator.ValidatorDescription;
import com.genia.toolbox.web.portlet.description.VariableDescription;
import com.genia.toolbox.web.portlet.description.impl.BooleanVariableDescription;
import com.genia.toolbox.web.portlet.description.impl.LongVariableDescription;

/**
 * The item settings panel.
 */
@SuppressWarnings("serial")
public class ItemSettingsPanel
    extends AbstractPanel<FormEditorGUI>
    implements DocumentListener, TableModelListener, ActionListener
{

  /**
   * The label name.
   */
  private JLabel labelName = null;

  /**
   * The text area name.
   */
  private JTextField textName = null;

  /**
   * The item name.
   */
  private JLabel labelItemName = null;

  /**
   * The text name item.
   */
  private JTextField textItemName = null;

  /**
   * The item description label.
   */
  private JLabel labelItemDescription = null;

  /**
   * The item description area.
   */
  private JTextArea areaItemDescription = null;

  /**
   * The label description.
   */
  private JLabel labelDescription = null;

  /**
   * The description area.
   */
  private JTextField textDescription = null;

  /**
   * Whether the item is enabled.
   */
  private JCheckBox checkIsEnabled = null;

  /**
   * The specific panel.
   */
  private JPanel specificPanel = null;

  /**
   * The displayed form model.
   */
  private FormModel model = null;

  /**
   * The parameter label.
   */
  private JLabel labelParameter = null;

  /**
   * The parameters table.
   */
  private JTable tableParameters = null;

  /**
   * The parameters table model.
   */
  private VariableTableModel tableParametersModel = null;

  /**
   * The entries panel.
   */
  private ItemEntrySettingsPanel panelEntries = null;

  /**
   * The validator panel.
   */
  private JPanel validatorPanel;

  /**
   * Validator list box.
   */
  private JComboBox validatorBox = null;

  /**
   * Description of a validator.
   */
  private JTextArea validatorAreaDescription = null;

  /**
   * The parameters validator table.
   */
  private JTable validatorTableParameters = null;

  /**
   * The parameters validator table model.
   */
  private VariableTableModel validatorTableParametersModel = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          GUI. The gui.
   */
  public ItemSettingsPanel(FormEditorGUI gui)
  {
    super(gui);
  }



  /**
   * Initialise the panel.
   */
  public void initialise()
  {

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    this.setLayout(layout);

    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 5, 5);
    this.add(this.createCommonSettingsPanel(), constraints);

    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    this.add(createValidatorSettingsPanel(), constraints);

    this.specificPanel = this.createSpecificPanel();
    this.specificPanel.setBorder(BorderFactory.createTitledBorder(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_SPECIFIC_BORDER)));
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    this.add(this.specificPanel, constraints);

    this.panelEntries = new ItemEntrySettingsPanel(this.getGui(), this, this, this);
    this.panelEntries.initialise();
    this.panelEntries.setBorder(BorderFactory.createTitledBorder(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_ENTRIES_BORDER)));
    constraints.gridx = 1;
    constraints.gridy = 4;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    this.add(this.panelEntries, constraints);

  }



  /**
   * Create the common panel.
   * 
   * @return the common panel.
   */
  public JPanel createCommonSettingsPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    panel.setBorder(BorderFactory.createTitledBorder(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_COMMON_BORDER)));

    this.labelName = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_COMMON_NAME));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.labelName, constraints);

    this.textName = new JTextField();
    this.textName.getDocument().addDocumentListener(this);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 5, 0, 0);
    panel.add(this.textName, constraints);

    this.labelItemName = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_COMMON_ITEM_NAME));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.labelItemName, constraints);

    this.textItemName = new JTextField();
    this.textItemName.setEditable(false);
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 0, 0);
    panel.add(this.textItemName, constraints);

    this.labelItemDescription = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_COMMON_ITEM_DESCRIPTION));
    this.labelItemDescription.setVerticalTextPosition(SwingConstants.TOP);
    this.labelItemDescription.setVerticalAlignment(SwingConstants.TOP);
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 3;
    constraints.weightx = 0;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTH;
    constraints.insets = new Insets(5, 0, 30, 0);
    panel.add(this.labelItemDescription, constraints);

    this.areaItemDescription = new JTextArea();
    this.areaItemDescription.setLineWrap(true);
    this.areaItemDescription.setWrapStyleWord(true);
    this.areaItemDescription.setFont(this.labelItemDescription.getFont());
    this.areaItemDescription.setEditable(false);
    constraints.gridx = 2;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 3;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.insets = new Insets(5, 5, 0, 0);
    panel.add(this.areaItemDescription, constraints);

    this.labelDescription = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_COMMON_DESCRIPTION));
    constraints.gridx = 1;
    constraints.gridy = 7;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.labelDescription, constraints);

    this.textDescription = new JTextField();
    this.textDescription.getDocument().addDocumentListener(this);
    constraints.gridx = 2;
    constraints.gridy = 7;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 0, 0);
    panel.add(this.textDescription, constraints);

    this.checkIsEnabled = new JCheckBox(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_COMMON_ISENABLED));
    constraints.gridx = 1;
    constraints.gridy = 8;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 30, 0, 0);
    panel.add(this.checkIsEnabled, constraints);

    return panel;
  }



  /**
   * Create the validator panel.
   * 
   * @return the validator panel.
   */
  public JPanel createValidatorSettingsPanel()
  {
    validatorPanel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    validatorPanel.setLayout(layout);

    validatorPanel.setBorder(BorderFactory.createTitledBorder(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_VALIDATOR_BORDER)));

    JLabel validatorName = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_VALIDATOR_TYPE));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.insets = new Insets(0, 5, 5, 5);
    validatorPanel.add(validatorName, constraints);

    validatorBox = new JComboBox(new DefaultComboBoxModel(this.getGui().getController().notifyDisplayValidatorDescriptions().toArray()));
    validatorBox.setRenderer(new ComboBoxRenderer(getGui()));
    validatorBox.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 5, 5);
    validatorPanel.add(validatorBox, constraints);

    JLabel validatorDescription = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_VALIDATOR_DESCRIPTION));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.insets = new Insets(0, 5, 5, 5);
    validatorPanel.add(validatorDescription, constraints);

    validatorAreaDescription = new JTextArea();
    validatorAreaDescription.setLineWrap(true);
    validatorAreaDescription.setWrapStyleWord(true);
    validatorAreaDescription.setFont(validatorDescription.getFont());
    validatorAreaDescription.setEditable(false);
    validatorAreaDescription.setRows(2);
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 0, 5, 5);
    validatorPanel.add(validatorAreaDescription, constraints);

    JLabel validatorParameters = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_VALIDATOR_PARAMETERS));
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.insets = new Insets(0, 5, 5, 5);
    validatorPanel.add(validatorParameters, constraints);

    String[] columnNames = new String[] { this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_VALIDATOR_PARAMETERS_NAME),
        this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_VALIDATOR_PARAMETERS_VALUE) };
    this.validatorTableParametersModel = new VariableTableModel(columnNames);
    this.validatorTableParametersModel.addTableModelListener(this);
    this.validatorTableParameters = new JTable(this.validatorTableParametersModel);
    VariableCellEditor cellEditor = new VariableCellEditor(this.getGui());
    VariableCellRenderer cellRenderer = new VariableCellRenderer(this.getGui());
    this.validatorTableParameters.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
    this.validatorTableParameters.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
    this.validatorTableParameters.getColumnModel().getColumn(0).setCellEditor(cellEditor);
    this.validatorTableParameters.getColumnModel().getColumn(1).setCellEditor(cellEditor);
    this.validatorTableParameters.setRowHeight(25);
    this.validatorTableParameters.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollpane = new JScrollPane(this.validatorTableParameters);
    constraints.gridx = 1;
    constraints.gridy = 4;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    validatorPanel.add(scrollpane, constraints);

    return validatorPanel;
  }



  /**
   * Create the parameters settings panel.
   * 
   * @return the parameters settings panel.
   */
  public JPanel createSpecificPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    this.labelParameter = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_SPECIFIC_LABEL_PARAMETERS));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.labelParameter, constraints);

    String[] columnNames = new String[] { this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_SPECIFIC_LABEL_PARAMETERS_NAME),
        this.getGui().getController().notifyTranslation(Messages.ITEM_SETTINGS_SPECIFIC_LABEL_PARAMETERS_VALUE) };
    this.tableParametersModel = new VariableTableModel(columnNames);
    this.tableParametersModel.addTableModelListener(this);
    this.tableParameters = new JTable(this.tableParametersModel);
    VariableCellEditor cellEditor = new VariableCellEditor(this.getGui());
    VariableCellRenderer cellRenderer = new VariableCellRenderer(this.getGui());
    this.tableParameters.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
    this.tableParameters.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
    this.tableParameters.getColumnModel().getColumn(0).setCellEditor(cellEditor);
    this.tableParameters.getColumnModel().getColumn(1).setCellEditor(cellEditor);
    this.tableParameters.setRowHeight(25);
    this.tableParameters.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollpane = new JScrollPane(this.tableParameters);
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 15, 0, 0);
    panel.add(scrollpane, constraints);

    return panel;
  }



  /**
   * Set the form.
   * 
   * @param form
   *          the form
   */
  public void setElement(FormModel form)
  {
    this.removeAllListeners();
    this.model = form;
    this.textName.setText(model.getName());
    this.textDescription.setText(model.getLabelKey());
    this.textItemName.setText(model.getElementName());
    if (model.getDescription() != null) {
      this.areaItemDescription.setText(this.getGui().getController().notifyTranslation(model.getDescription()));
    }
    this.checkIsEnabled.setSelected(model.isEnabled());

    this.tableParametersModel.clear();
    for (VariableModel variable : model.getParameters()) {
      this.tableParametersModel.addVariable(variable);
    }

    if (!form.canHaveValidator()) {
      validatorPanel.setVisible(false);
    }
    else {
      validatorPanel.setVisible(true);
      for (int i = 0; i < validatorBox.getItemCount(); ++i) {
        if (((ValidatorDescription) validatorBox.getItemAt(i)).getClassName().equals(model.getValidatorClassName())) {
          this.validatorBox.setSelectedItem(validatorBox.getItemAt(i));
          break;
        }
      }
      this.validatorAreaDescription.setText(this.getGui().getController().notifyTranslation(model.getValidatorDescriptionName()));
      this.validatorTableParametersModel.clear();
      for (VariableModel variable : model.getValidatorParameters()) {
        this.validatorTableParametersModel.addVariable(variable);
      }
    }

    if (form.hasEntries()) {
      this.panelEntries.setVisible(true);
      this.panelEntries.setDisplayedValues(model.getEnumEntry(), model.getEntries());
    }
    else {
      this.panelEntries.setVisible(false);
    }
    this.validateSettings();
    this.addAllListeners();
  }



  /**
   * Get the form.
   * 
   * @return the form
   */
  public FormModel getElement()
  {
    FormModel updatedModel = this.model;
    updatedModel.setName(this.textName.getText());
    updatedModel.setLabelKey(this.textDescription.getText());
    updatedModel.setIsEnabled(this.checkIsEnabled.isSelected());
    List<VariableModel> parameters = new ArrayList<VariableModel>();
    int rows = this.tableParameters.getRowCount();
    for (int row = 0; row < rows; row++) {
      VariableModel variable = this.tableParametersModel.getValueAt(row);
      if (variable != null) {
        parameters.add(variable);
      }
    }
    updatedModel.setParameters(parameters);

    if (updatedModel.canHaveValidator()) {
      Description description = (Description) this.validatorBox.getSelectedItem();
      updatedModel.setValidatorClassName(description.getClassName());
      updatedModel.setValidatorDescriptionName(description.getDescription().getKey());
      List<VariableModel> validatorParameters = new ArrayList<VariableModel>();
      rows = this.validatorTableParameters.getRowCount();
      for (int row = 0; row < rows; row++) {
        VariableModel variable = this.validatorTableParametersModel.getValueAt(row);
        if (variable != null) {
          validatorParameters.add(variable);
        }
      }
      updatedModel.setValidatorParameters(validatorParameters);
    }

    if (updatedModel.hasEntries()) {
      updatedModel.setEnumEntry(this.panelEntries.getEnumeration());
      updatedModel.setEntries(this.panelEntries.getEntries());
    }
    return updatedModel;
  }



  /**
   * Handle the change document events.
   * 
   * @param e
   *          the document event.
   */
  public void changedUpdate(DocumentEvent e)
  {
    // Do nothing.
  }



  /**
   * Handle the insert document events.
   * 
   * @param e
   *          the document event.
   */
  public void insertUpdate(DocumentEvent e)
  {
    this.validateSettings();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyElementSettingsChanged(this.getElement());
  }



  /**
   * Handle the remove document events.
   * 
   * @param e
   *          the document event.
   */
  public void removeUpdate(DocumentEvent e)
  {
    this.validateSettings();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyElementSettingsChanged(this.getElement());
  }



  /**
   * Handle the table change events.
   * 
   * @param e
   *          the table event.
   */
  public void tableChanged(TableModelEvent e)
  {
    this.validateSettings();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyElementSettingsChanged(this.getElement());
  }



  /**
   * Handle the action event.
   * 
   * @param e
   *          the action event.
   */
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source.equals(this.validatorBox)) {

      validatorTableParametersModel.clear();

      Description description = (Description) this.validatorBox.getSelectedItem();
      if (description != null) {
        validatorAreaDescription.setText(this.getGui().getController().notifyTranslation(description.getDescription()));

        for (VariableDescription variableDescription : description.getVariableDescriptions()) {
          boolean isLongValue = false;
          boolean isBooleanValue = false;
          if (variableDescription instanceof LongVariableDescription) {
            isLongValue = true;
          }
          else if (variableDescription instanceof BooleanVariableDescription) {
            isBooleanValue = true;
          }
          VariableModel variableModel = new VariableModel(variableDescription.getName(), variableDescription.getDescription(), "", variableDescription.getValues(), variableDescription.isMandatory(),
              isLongValue, isBooleanValue);

          removeAllListeners();

          validatorTableParametersModel.addVariable(variableModel);

          addAllListeners();

        }

      }

      notifyChanges();

    }
  }



  /**
   * Check whether the settings are valid.
   */
  public void validateSettings()
  {
    if (this.textName.getText() == null || this.textName.getText().trim().length() == 0) {
      this.labelName.setForeground(AbstractGUI.COLOR_TEXT_ERROR);
    }
    else {
      this.labelName.setForeground(AbstractGUI.COLOR_TEXT);
    }
    if (this.textDescription.getText() == null || this.textDescription.getText().trim().length() == 0) {
      this.labelDescription.setForeground(AbstractGUI.COLOR_TEXT_ERROR);
    }
    else {
      this.labelDescription.setForeground(AbstractGUI.COLOR_TEXT);
    }
    this.panelEntries.validateSettings();
  }



  /**
   * Remove all listeners.
   */
  public void removeAllListeners()
  {
    this.textName.getDocument().removeDocumentListener(this);
    this.textDescription.getDocument().removeDocumentListener(this);
    this.tableParametersModel.removeTableModelListener(this);
    this.validatorBox.removeActionListener(this);
    this.validatorTableParametersModel.removeTableModelListener(this);
  }



  /**
   * Add all listeners.
   */
  public void addAllListeners()
  {
    this.textName.getDocument().addDocumentListener(this);
    this.textDescription.getDocument().addDocumentListener(this);
    this.tableParametersModel.addTableModelListener(this);
    this.validatorBox.addActionListener(this);
    this.validatorTableParametersModel.addTableModelListener(this);
  }



  /**
   * Notify changes.
   */
  public void notifyChanges()
  {
    this.validateSettings();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyElementSettingsChanged(this.getElement());
  }

}
